package com.mdp.workflow.re.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.Tips;
import com.mdp.core.service.BaseService;
import com.mdp.workflow.re.entity.ProcdefTag;
import com.mdp.workflow.re.mapper.ProcdefTagMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;
/**
 * 父类已经支持增删改查操作,因此,即使本类什么也不写,也已经可以满足一般的增删改查操作了.<br> 
 * 组织 com.mdp  顶级模块 workflow 大模块 re 小模块 <br>
 * 实体 ProcdefTag 表 mdp_re_procdef_tag 当前主键(包括多主键): id; 
 ***/
@Service
public class ProcdefTagService extends BaseService<ProcdefTagMapper,ProcdefTag> {
	static Logger logger =LoggerFactory.getLogger(ProcdefTagService.class);

	/**
	 * 自定义查询，支持多表关联
	 * @param page 分页条件
	 * @param ew 一定要，并且必须加@Param("ew")注解
	 * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
	 * @return
	 */
	public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
		return baseMapper.selectListMapByWhere(page,ew,ext);
	}


	public void batchInsertTags(String tagIds,String tagNames,String branchId,String userid,String procDefId){
		if(!StringUtils.isEmpty(tagIds) && !StringUtils.isEmpty(tagNames) ) {
			String[] tagIdsArr=tagIds.split(",");
			String[] tagIdsNamesArr=tagNames.split(",");
			if(tagIdsArr.length==tagIdsNamesArr.length) {
				List<ProcdefTag> tags=new ArrayList<>();
				int index=0;
				for (String tagId : tagIdsArr) {
					ProcdefTag procdefTag =new ProcdefTag();
					procdefTag.setAddUserid(userid);
					procdefTag.setProcDefId(procDefId);
					procdefTag.setBranchId(branchId);
					procdefTag.setAddUserid(userid);
					procdefTag.setTagId(tagId);
					procdefTag.setId(this.createKey("id"));

					procdefTag.setTagName(tagIdsNamesArr[index]);
					index=index+1;
				}
				this.batchInsert(tags);
			}
		}
	}

	public Tips batchInsertOrDeleteTags(String branchId,String procDefId,String userid,List<ProcdefTag> tags) {
		Tips tips =new Tips("成功更新标签");
		Map<String,Object> m=new HashMap<>();
		m.put("branchId", branchId);
		m.put("procDefId", procDefId);
		List<String> tagIds=new ArrayList<>();
		for (ProcdefTag tag : tags) {
			tagIds.add(tag.getTagId());
		}
		m.put("tagIds", tagIds);
		ProcdefTag tagQuery=new ProcdefTag();
		tagQuery.setProcDefId(procDefId);
		List<ProcdefTag> dbTags=this.selectListByWhere(tagQuery);
		List<ProcdefTag> needDeleteList=new ArrayList<>();
		List<ProcdefTag> needAddList=new ArrayList<>();
		Date date=new Date();
		boolean existsDbTags=dbTags!=null && dbTags.size()>0;
		if(tags==null || tags.size()<=0) {//清空标签
			//清除流程实例标签表
			if(existsDbTags) {
				this.batchDelete(dbTags);
			}
			//TODO 清除流程实例参数表的tagIds，tagNames字段
			tips.setMsg("删除标签成功");
			return tips;
		}
		for (ProcdefTag tag : tags) {
			boolean exists=false;
			if(existsDbTags) {
				for (ProcdefTag dbTag : dbTags) {
					if(dbTag.getTagId().equals(tag.getTagId())) {
						exists=true;
						needDeleteList.add(dbTag);
						break;
					}
				}
			}
			if(exists==false) {
				tag.setBranchId(branchId);
				tag.setProcDefId(procDefId);
				tag.setId(this.createKey("id"));
				tag.setAddTime(date);
				tag.setAddUserid(userid);
				needAddList.add(tag);
			}
		}
		if(needAddList.size()>0) {
			this.batchInsert(needAddList);
		}
		if(needDeleteList.size()>0) {
			this.batchDelete(needDeleteList);
		}
		return tips;

	}
}

